
function addAdminCourse(){

	//get the date from form
	var courseName = document.getElementById('coursename').value;
	var data = "coursename=" + encodeURIComponent(courseName);
	
	if (courseName.trim() == ''){
		alert("You must enter a course name");
		return;
	}
	XMLRequestPostQ("courses/adminservices.php?method=addcourse", data, addAdminCourseCallback);
	
}

function addAdminCourseCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	//clear course name box
	document.getElementById('coursename').value = "";

	//get title & date & id from xmlDoc and add to 
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var courseId = idNode[0].firstChild.nodeValue;
	
	var courseNode = xmlDoc.documentElement.getElementsByTagName('coursename');
	var courseName = courseNode[0].firstChild.nodeValue;
	
	var cRow = document.createElement("tr");
	cRow.setAttribute("id","courserow"+courseId);
	
	var nameF = document.createElement("td");
	nameF.setAttribute("id","courserow"+courseId+"edit");
	nameF.appendChild(document.createTextNode(courseName));
	cRow.appendChild(nameF);
	
	var optionF = document.createElement("td");
	optionF.setAttribute("id","courserow"+courseId+"options");
	
	var editSpan = document.createElement("span");
	editSpan.setAttribute("id","editspan"+courseId);
	if(editSpan.addEventListener){ // Mozilla, Netscape, Firefox
		editSpan.addEventListener('click', function(){
			editAdminCourse(courseId);
			}, false);
	} else { // IE
		editSpan.attachEvent('onclick', function(){
			editAdminCourse(courseId);
			});
	}
	changeNodeClass(editSpan,'remove');
	editSpan.appendChild(document.createTextNode("[edit]"));
	optionF.appendChild(editSpan);
	
	var deleteSpan = document.createElement("span");
	if(deleteSpan.addEventListener){ // Mozilla, Netscape, Firefox
		deleteSpan.addEventListener('click', function(){deleteAdminCourse(courseId,courseName);}, false);
	} else { // IE
		deleteSpan.attachEvent('onclick', function(){deleteAdminCourse(courseId,courseName);});
	}
	changeNodeClass(deleteSpan,'remove');
	deleteSpan.appendChild(document.createTextNode("[delete]"));
	optionF.appendChild(deleteSpan);
	
	cRow.appendChild(optionF);
	
	var brother = document.getElementById("addcourserow");
	brother.parentNode.insertBefore(cRow,brother);
	
}

function deleteAdminCourse(courseId,courseTitle){
	if (confirm("Are you sure you want to delete the course '"+courseTitle+"'?")){
		XMLRequestQ("courses/adminservices.php?method=deletecourse&cid=" + courseId, deleteAdminCourseCallback);
	}
}

function deleteAdminCourseCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	// extract the Id
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var courseId = idNode[0].firstChild.nodeValue;
	
	// extract whether or not deleted
	var deletedNode = xmlDoc.documentElement.getElementsByTagName('deleted');
	var deleted = deletedNode[0].firstChild.nodeValue;
	
	if(deleted != "true"){
		var errorNode = xmlDoc.documentElement.getElementsByTagName('error');
		var error = errorNode[0].firstChild.nodeValue;
		alert("Error: " + error);
		return;
	}
	//remove the node
	var row = document.getElementById('courserow'+courseId);
	row.parentNode.removeChild(row);
}


function editAdminCourse(courseId){
	//get the current course title
	data = "cid="+encodeURIComponent(courseId);
	XMLRequestPostQ("courses/adminservices.php?method=getcourse", data, editAdminCourseCallback);
}
	
function editAdminCourseCallback(xmlDoc){

	if(!xmlDoc){
		return;
	}
	
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var courseId = idNode[0].firstChild.nodeValue;
	
	var courseNameNode = xmlDoc.documentElement.getElementsByTagName('coursename');
	var courseName = courseNameNode[0].firstChild.nodeValue;
	
	// get the coursetitle field and change to be an input field
	var cell = document.getElementById('courserow'+courseId+'edit');
	cell.innerHTML = "";
	
	var inputField = document.createElement("input");
	inputField.setAttribute("name","edit"+courseId);
	inputField.setAttribute("id","edit"+courseId);
	inputField.setAttribute("type","text");
	inputField.setAttribute("size","40");
	inputField.setAttribute("value",courseName);
	cell.appendChild(inputField);
	
	var saveBtn = document.createElement("input");
	saveBtn.setAttribute("type","button");
	saveBtn.setAttribute("value","Save");
	if(saveBtn.addEventListener){ // Mozilla, Netscape, Firefox
		saveBtn.addEventListener('click', function(){saveAdminCourse(courseId);}, false);
	} else { // IE
		saveBtn.attachEvent('onclick', function(){saveAdminCourse(courseId);});
	}
	cell.appendChild(saveBtn);
}

function saveAdminCourse(courseId){
 	var data = "cid=" + encodeURIComponent(courseId);
	
	//get the name from form
	var courseName = document.getElementById('edit' + courseId).value;
	data += "&coursename=" + encodeURIComponent(courseName);
	
	XMLRequestPostQ("courses/adminservices.php?method=editcourse", data, saveAdminCourseCallback);
}

function saveAdminCourseCallback(xmlDoc){
	if(!xmlDoc){
		return;
	}
	
	// extract the Id
	var idNode = xmlDoc.documentElement.getElementsByTagName('ident');
	var courseId = idNode[0].firstChild.nodeValue;
	
	// extract the Id
	var nameNode = xmlDoc.documentElement.getElementsByTagName('coursename');
	var courseName = nameNode[0].firstChild.nodeValue;
	
	// extract whether or not edited
	var editedNode = xmlDoc.documentElement.getElementsByTagName('edited');
	var edited = editedNode[0].firstChild.nodeValue;
	if(edited != "true"){
		var errorNode = xmlDoc.documentElement.getElementsByTagName('error');
		var error = errorNode[0].firstChild.nodeValue;
		alert("Error: " + error);
		return;
	};
	
	var cell = document.getElementById('courserow'+courseId+'edit');
	cell.innerHTML = courseName;
}
